import {
    initView,
    createRecord,
    createshLis
} from '../views/staffRecordView.js';

import {} from '../base/observer.js';
var cw = require("../components/winLocation.js");
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;


var token, loginName, openid, loginType, orderId, goodsAmount;
var errorObject;
$(document).on("pageinit", "#staffRecord_page", controllerInit);

// 交易查询控制器初始化
function controllerInit() {
    cw.checkBrowser();
    initValue();
    initView();
    subscribeRecordData();
    getMerList();
    bindEvent();
}

// 关联的商户列表
function getMerList() {
    var srParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType
    };
    $.post(MerRelation_URL, srParameters, function(data) {
        console.dir(data);
        if (data.merchant_Qw_response && data.merchant_Qw_response.mer_result_arrays.mer_result) {
            var lidatas = data.merchant_Qw_response.mer_result_arrays.mer_result;
            createshLis(lidatas);
            $("#shlist>li").off("touchend");
            $("#shlist>li").on("touchend", selectMerRecord);
        }

    });
    /*    var lidatas = [{
            "merName": "九一九919",
            "merNo": 999112153990001
        }, {
            "merName": "分公司商户2",
            "merNo": 999331047840001
        }, {
            "merName": "浙江分期S_TEST",
            "merNo": 999331053090001
        }];
        createshLis(lidatas);  
    $("#shlist>li").on("touchstart", selectMerRecord);
    */
}

// 选择商户查询记录
function selectMerRecord(e) {
    var merNo = $(e.target).attr("data-merNo");
    $("#billText>span:first-child").text($(e.target).text());
    subscribeRecordData(merNo);
    setTimeout(function() {
        $("#merBox").fadeOut(100);
    }, 400);
}


// 取交易记录数据
function subscribeRecordData(mer_id) {

    var srParameters = {
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType,
        "mer_id": mer_id
    };

    $.post(CashierTrade_URL, srParameters, TRback);

}

// 交易记录回调函数
function TRback(trData) {

    if (trData.login_response) {
        haslogin(trData);
        return;
    }
    if (trData.tltdtrans_response && trData.tltdtrans_response.trans_info_arrays && trData.tltdtrans_response.trans_info_arrays.trans_info) {
        var $cells = $("#record_cells");
        var transInfos = trData.tltdtrans_response.trans_info_arrays.trans_info
        if (transInfos.length == 0) {
            errorObject.showErroDialog("没有交易记录");
        } else {
            if (transInfos.length == undefined) { // 只有一条记录时返回的是对象不是数组处理
                var oneTransInfos = [];
                oneTransInfos.push(transInfos);
                transInfos = oneTransInfos;
            }
            createRecord(transInfos, $cells);
        }

    } else if (trData.tltdtrans_response && trData.tltdtrans_response.resp_msg) {
        errorObject.showErroDialog(trData.tltdtrans_response.resp_msg);
    } else {
        errorObject.showErroDialog("查询交易记录失败");
    }


}


// 点击订单详情列表到订单详情页
function linkOrderDetail(e) {
    var trans_seq;
    if ($(e.target).attr("data-goods_Amount")) {
        if ($(e.target).hasClass("weui-btn_plain-disabled")) {
            return;
        } else {
            goodsAmount = $(e.target).attr("data-goods_Amount");
            orderId = $(e.target).attr("data-order_id");
        }
    } else if ($(e.target).attr("data-trans_seq")) {
        trans_seq = $(e.target).attr("data-trans_seq");
    } else {
        var theParnet = $(e.target).parents("[data-trans_seq]");
        if (theParnet[0]) {
            trans_seq = theParnet.attr("data-trans_seq");
        }
    }
    if (trans_seq) {
        location.href = "./orderDetail2.html?trans_seq=" + trans_seq;
    }
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}

// 绑定事件
function bindEvent() {
    $("#record_cells").on("click", linkOrderDetail);

}

// 初始化变量
function initValue() {
    errorObject = new errorDialog();
    getSessionStorage();
    logoutObject = new Logout();
}



// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    console.log(token, loginName, openid, loginType);
}